package com.ogawa.chair7808.ui.activity

import android.os.Handler
import android.os.Message
import android.util.Log
import android.view.View
import android.widget.RelativeLayout
import android.widget.ScrollView
import com.blankj.utilcode.util.LogUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import com.ogawa.base.Constant.CommmandNum
import com.ogawa.base.Constant.Constant
import com.ogawa.base.Constant.Constants
import com.ogawa.base.utils.CommonUtil
import com.ogawa.base.utils.DownTimer
import com.ogawa.base.utils.StringUtils
import com.ogawa.chair7808.R
import com.ogawa.chair7808.cache.*
import com.ogawa.chair7808.interfaces.OnTimeChoose
import com.ogawa.chair7808.manager.DataManager.Companion.dataManager
import com.ogawa.chair7808.utils.Activity7808Utils
import com.ogawa.chair7808.view.MoveLayout
import com.ogawa.chair7808.widget.*
import com.ska.skautils.utilcode.util.ActivityUtils
import java.lang.ref.WeakReference
import java.util.*
import kotlin.concurrent.schedule

/**
 * 专业按摩 按摩页面
 * Created by FHB on 2018/3/27.
 */
class ProMasDetailActivity7808 : BaseMqtt7808Activity(), View.OnClickListener, MoveLayout.OnChooseAction, OnTimeChoose //        ,IProMasDetailView7506 ,TimeObserver.onTimeUpdataLis
//        , CloseDetailObserver.onCloseDetail
{
    private lateinit var mLl3D: RelativeLayout
    private lateinit  var mLlStrong: RelativeLayout
    private lateinit  var mLlSpeed: RelativeLayout
    private lateinit  var mLlAir: RelativeLayout
    private lateinit  var mLayoutBody7808: LayoutBody7808
    private lateinit  var mLayoutFunction: LayoutFunctionView7808
    private var mName = ""

    //    private ProMasDetailPresenter7506 mPresenter;
    private lateinit var mLayout3D: LayoutGears6_7808
    private lateinit var mLayoutStrong: LayoutGears5_7808
    private lateinit var mLayoutSpeed: LayoutGears5_7808
    private lateinit var mLayoutAir: LayoutGears5_7808
    private val name = arrayOf("3D", "强度", "速度", "气压强度")
    private lateinit var mLayout: MoveLayout
    private lateinit var mScroll: ScrollView
    private lateinit var mTvTime: ViewTvTime
    private  var myHandler: MyHandler ?=null

    //    private MyCountDownTimer timer;
    private val INTERVAL = 1000L
    private val mTime: Long = 0
    private val mTimer: DownTimer? = null
    override fun setLayoutId(): Int {
        return R.layout.activity_pro_massage_detail_7808
    }

    override fun initTitleBar() {
        super.initTitleBar()
        titleBar.setGreenBackBotton()
        titleBar.setTitle(autoProgramName)
        setBackGround(R.mipmap.bg)
        setmName("ProMasDetailActivity7506")
    }

    override fun initView(layoutId: Int) {
        super.initView(layoutId)
        initView()
        setTimeListener(this)


    }

    private fun initView() {
        Log.e("is7506a", "ProMasDetailActivity7506")
        mName = intent.getStringExtra("name")
        mLl3D = findViewById(R.id.layout_3d_7506)
        mLlStrong = findViewById(R.id.linear_strong_7506)
        mLlSpeed = findViewById(R.id.linear_speed_7506)
        mLlAir = findViewById(R.id.linear_strong_air_7506)
        mTvTime = findViewById(R.id.showTime_7506)
        mLayoutBody7808 = findViewById(R.id.layout_body_view_7506)
        mLayoutFunction = findViewById(R.id.activity_pro_mas_functionview_7506)
        mScroll = findViewById(R.id.activity_detail_scroll_7506)
        mLayout = findViewById(R.id.detail_move_layout_7506)
        mLayout.setData(name)
        mLayout.setScollView(mScroll)
        mLayout.setAction(this)
        mLayout3D = findViewById(R.id.layout_3d_7506)
        mLayoutStrong = findViewById(R.id.linear_strong_7506)
        mLayoutSpeed = findViewById(R.id.linear_speed_7506)
        mLayoutAir = findViewById(R.id.linear_strong_air_7506)
        mLayout3D.setType(1)
        mLayoutStrong.setType(2)
        mLayoutSpeed.setType(3)
        mLayoutAir.setType(4)
        mTvTime.setOnClickListener(this)

        myHandler = MyHandler(this)
        updateView()
    }

    override fun onDestroy() {
        super.onDestroy()
        mLayoutBody7808!!.onDestory()
        mLayoutFunction!!.onDestory()
    }

    override fun onClick(v: View) {
        val i = v.id
         if (i == R.id.showTime_7506) {
            if (CommonUtil.isFastClick()) {
                return
            } else {
                showDialog()
            }
        }
    }

    override fun onPause() {
        super.onPause()
        isHandOutProMasDetailActivity = true
    }

    override fun onBackPressed() {
        super.onBackPressed()
        Activity7808Utils.startProfessionMassaActivity(this)
        finish()
    }

    override fun onChoose(position: Int) {
        mLayout!!.setTectColor(position)
        when (position) {
            0 -> {
                mLlStrong!!.visibility = View.GONE
                mLl3D!!.visibility = View.VISIBLE
                mLlSpeed!!.visibility = View.GONE
                mLlAir!!.visibility = View.GONE
            }
            1 -> {
                mLlStrong!!.visibility = View.VISIBLE
                mLl3D!!.visibility = View.GONE
                mLlSpeed!!.visibility = View.GONE
                mLlAir!!.visibility = View.GONE
            }
            2 -> {
                mLlStrong!!.visibility = View.GONE
                mLl3D!!.visibility = View.GONE
                mLlSpeed!!.visibility = View.VISIBLE
                mLlAir!!.visibility = View.GONE
            }
            3 -> {
                mLlStrong!!.visibility = View.GONE
                mLl3D!!.visibility = View.GONE
                mLlSpeed!!.visibility = View.GONE
                mLlAir!!.visibility = View.VISIBLE
            }
        }
    }

    override fun onTimeListener(time: String) {
        if (time == Constants.TIME_5) {
            dataManager.sendCommond(CommmandNum.TIME_5, "5")
        } else if (time == Constants.TIME_10) {
            dataManager.sendCommond(CommmandNum.TIME_10, "10")
        } else if (time == Constants.TIME_15) {
            dataManager.sendCommond(CommmandNum.TIME_15, "15")
        } else if (time == Constants.TIME_20) {
            dataManager.sendCommond(CommmandNum.TIME_20, "20")
        } else if (time == Constants.TIME_25) {
            dataManager.sendCommond(CommmandNum.TIME_25, "25")
        } else if (time == Constants.TIME_30) {
            dataManager.sendCommond(CommmandNum.TIME_30, "30")
        }

//        startTimer(Integer.valueOf(time.substring(0,time.indexOf(":")).trim()),Integer.valueOf(time.substring(time.length()-2,time.length() )));
    }

    fun updateView() {
        val device7808StateBean = deviceStatusBean
        mLayout3D!!.setShow(StringUtils.getValue(device7808StateBean._3DPosIconBean))
        mLayoutStrong!!.setShow(StringUtils.getValue(device7808StateBean.massageStrength))
        mLayoutSpeed!!.setShow(StringUtils.getValue(device7808StateBean.massageSpeed))
        mLayoutAir!!.setShow(StringUtils.getValue(device7808StateBean.airIntensity))
        val name = "" + autoProgramName
        titleBar.setTitle(name)
        Timer().schedule(500){
            if(!isFinishShow){
                LogUtils.e("___detialggg+===${isHandProgram}")
                if (!isautoProgram) {
                    if (!isHandProgram) {
                        myHandler!!.postDelayed({
                            if (ActivityUtils.getTopActivity() == this@ProMasDetailActivity7808) {
                                ActivityUtils.finishToActivity(Home7808Activity7808::class.java, false)
                            }
                        }, 500)
                    } else {
                        LiveEventBus.get(Constant.LIVE_EVENT_BUS_KEY_CLOSE_PRO).post(0)
                        Activity7808Utils.startAdvancedMassageActivity(this@ProMasDetailActivity7808)
                        finish()
                    }
                }
            }

        }

    }

    public override fun changeViewByMqttMessage() {
        super.changeViewByMqttMessage()
        updateView()
    }

    private inner class MyHandler internal constructor(mProMasDetailActivity: ProMasDetailActivity7808) : Handler() {
        var mProMasDetailActivityWeakReference: WeakReference<ProMasDetailActivity7808> = WeakReference(mProMasDetailActivity)
        override fun handleMessage(msg: Message) {
            val mHomeActivity = mProMasDetailActivityWeakReference.get()
            if (mHomeActivity != null) {
                when (msg.what) {
                    1 -> mLayout3D!!.setShow(msg.obj as Int)
                }
            }
        }

    }
}